import os
import support

from tasks import TaskManager as _TaskManager
from receipts import ReceiptManager as _ReceiptManager

from events import InformationEvent as _InformationEvent
from tossfiles import Tossfile
from users import User
from roles import Role
from flags import Tossflag

__version__ = "0.0.1"
__author__ = "Matt Pizzimenti"
__author_email__ = "mjpizz@gmail.com"

local = Role("$")
"""Provides quick access to loggable execution of commands as the local
user on the local machine"""

def task(fn1 = None):
    """Decorator that converts a no-argument function into a pytoss
    Task that can be executed as part of a task stack"""
    if fn1:
        return _TaskManager.get_instance().create_task(task_callback = fn1).execute
    else:
        def wrapper(fn2):
            return _TaskManager.get_instance().create_task(task_callback = fn2).execute
        return wrapper

def log(message):
    """Logs a simple information event to the receipts"""
    _ReceiptManager.get_instance().log_event( _InformationEvent( message ) )

def path(path, *additional_path_components):
    """interprets the given path as relative to the CALLER
    of this function, and returns that path as an absolute path"""
    # TODO: check for os.path.isabs(), since this is not necessarily relative anyway
    prefix = support.absolute_directory_of_caller()
    path = os.path.normpath(path)
    additional_path_components = [os.path.normpath(component) for component in additional_path_components]
    fullpath = os.path.join(prefix, path, *additional_path_components)
    fullpath = os.path.expandvars(fullpath)
    fullpath = os.path.normpath(fullpath)
    return fullpath
